//
//  CCSimpleFileOutputRedirectionController.m
//  CCLogSystem
//
//  Created by Chun Ye on 10/30/14.
//  Copyright (c) 2014 Chun Tips. All rights reserved.
//

#import "CCSimpleFileOutputRedirectionController.h"

@interface CCSimpleFileOutputRedirectionController ()

@property (nonatomic, strong) NSFileHandle *inputFileHandleDup;

@end

@implementation CCSimpleFileOutputRedirectionController
@synthesize inputFileHandle;
@synthesize outputFileHandle;

- (void)startRecordLog
{
    int inputFileDescriptionDup = dup(self.inputFileHandle.fileDescriptor);
    dup2(self.outputFileHandle.fileDescriptor, self.inputFileHandle.fileDescriptor);
    
    self.inputFileHandleDup = [[NSFileHandle alloc] initWithFileDescriptor:inputFileDescriptionDup];
}

- (void)stopRecordLog
{
    dup2(self.inputFileHandleDup.fileDescriptor, self.inputFileHandle.fileDescriptor);
    self.inputFileHandleDup = nil;
}

@end
